package com.collect.zambia.zhiScene.zhiScriptLib;

import android.content.Context;

import com.collect.zambia.zhiBase.BasePresenter;
import com.collect.zambia.zhiBase.BaseSubscriber;
import com.collect.zambia.zhiBean.zhiReceive.ZHIRScriptLibListEntity;
import com.collect.zambia.zhiBean.zhiSend.ZHISendEntity;
import com.collect.zambia.zhiNetApi.ZHIHttpApiProvider;
import com.collect.zambia.zhiNetApi.ZHIHttpResponse;
import com.collect.zambia.zhiUtils.RxSchedulers;

import javax.inject.Inject;

public class ZHIScriptLibPresenter extends BasePresenter<ZHIScriptLibContract.View> implements ZHIScriptLibContract.Presenter {

    @Inject
    public ZHIScriptLibPresenter() {

    }


    @Override
    public void getAppWords(Context context, final ZHISendEntity zhiSendEntity) {
        BaseSubscriber<ZHIHttpResponse<ZHIRScriptLibListEntity>> subscriber = new
                BaseSubscriber<ZHIHttpResponse<ZHIRScriptLibListEntity>>(context, this, true) {
                    @Override
                    protected void onSuccess(Object object) {
                        try {
                            ZHIRScriptLibListEntity scriptLibListEntity = (ZHIRScriptLibListEntity) object;
                            mView.onGetAppWords(scriptLibListEntity);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }

                    @Override
                    protected void onFailure(int code, String messge) {
                        showMessage(messge);
                        mView.onGetErrorInfo();
                    }
                };
        ZHIHttpApiProvider.getApi().appWords(zhiSendEntity)
                .compose(RxSchedulers.<ZHIHttpResponse<ZHIRScriptLibListEntity>>applySchedulers())
                .compose(mView.<ZHIHttpResponse<ZHIRScriptLibListEntity>>bindToLife())
                .subscribe(subscriber);
    }
}
